import CALENDAR from './calendar.js'

class Calendar {
  constructor({
    date,
    selected,
    startDate,
    endDate,
    range,
    planList
  } = {}) {
    // 当前日期
    this.date = this.getDate(new Date()) // 当前初入日期
    // 打点信息
    this.selected = selected || [];
    //周计划
    this.planList = planList || [];
    // 范围开始
    this.startDate = startDate
    // 范围结束
    this.endDate = endDate
    this.range = range
    // 多选状态
    this.cleanMultipleStatus()
    // 每周日期
    this.weeks = {}
    // this._getWeek(this.date.fullDate)
  }
  /**
   * 设置日期
   * @param {Object} date
   */
  setDate(date) {
    this.selectDate = this.getDate(date)
    this._getWeek(this.selectDate.fullDate)
  }

  /**
   * 清理多选状态
   */
  cleanMultipleStatus() {
    this.multipleStatus = {
      before: '',
      after: '',
      data: []
    }
  }

  /**
   * 重置开始日期
   */
  resetSatrtDate(startDate) {
    // 范围开始
    this.startDate = startDate

  }

  /**
   * 重置结束日期
   */
  resetEndDate(endDate) {
    // 范围结束
    this.endDate = endDate
  }

  /**
   * 获取任意时间
   */
  getDate(date, AddDayCount = 0, str = 'day') {
    if (!date) {
      date = new Date()
    }
    if (typeof date !== 'object') {
      date = date.replace(/-/g, '/')
    }
    const dd = new Date(date)
    switch (str) {
      case 'day':
        dd.setDate(dd.getDate() + AddDayCount) // 获取AddDayCount天后的日期
        break
      case 'month':
        if (dd.getDate() === 31 && AddDayCount > 0) {
          dd.setDate(dd.getDate() + AddDayCount)
        } else {
          const preMonth = dd.getMonth()
          dd.setMonth(preMonth + AddDayCount) // 获取AddDayCount天后的日期
          const nextMonth = dd.getMonth()
          // 处理 pre 切换月份目标月份为2月没有当前日(30 31) 切换错误问题
          if (AddDayCount < 0 && preMonth !== 0 && nextMonth - preMonth > AddDayCount) {
            dd.setMonth(nextMonth + (nextMonth - preMonth + AddDayCount))
          }
          // 处理 next 切换月份目标月份为2月没有当前日(30 31) 切换错误问题
          if (AddDayCount > 0 && nextMonth - preMonth > AddDayCount) {
            dd.setMonth(nextMonth - (nextMonth - preMonth - AddDayCount))
          }
        }
        break
      case 'year':
        dd.setFullYear(dd.getFullYear() + AddDayCount) // 获取AddDayCount天后的日期
        break
    }
    const y = dd.getFullYear()
    const m = dd.getMonth() + 1 < 10 ? '0' + (dd.getMonth() + 1) : dd.getMonth() + 1 // 获取当前月份的日期，不足10补0
    const d = dd.getDate() < 10 ? '0' + dd.getDate() : dd.getDate() // 获取当前几号，不足10补0
    return {
      fullDate: y + '-' + m + '-' + d,
      year: y,
      month: m,
      date: d,
      day: dd.getDay()
    }
  }


  /**
   * 获取上个月剩余的天数
   * @param {Object} firstDay
   * @param {Object} full
   */
  _getLastMonthDays(firstDay, full) {
    let dateArr = []
    for (let i = firstDay; i > 0; i--) {
      const beforeDate = new Date(full.year, full.month - 1, -i + 1).getDate()
      const month = full.month - 1
      const fullDate = full.year + '-' + (month < 10 ?
        '0' + month : month) + '-' + beforeDate
      dateArr.push({
        fullDate,
        date: beforeDate,
        month: month,
        lunar: this.getlunar(full.year, full.month - 1, beforeDate),
        disable: true
      })
    }
    return dateArr
  }
  /**
   * 获取本月天数
   */
  _currentMonthDys(dateData, full) {
    let dateArr = []
    let fullDate = this.date.fullDate
    for (let i = 1; i <= dateData; i++) {
      let nowDate = full.year + '-' + (full.month < 10 ?
        full.month : full.month) + '-' + (i < 10 ?
        '0' + i : i)
      // 是否今天
      let isDay = fullDate === nowDate
      // 获取打点信息
      let info = this.selected && this.selected.find((item) => {
        if (this.dateEqual(nowDate, item.date)) {
          return item
        }
      })

      // 日期禁用
      let disableBefore = true
      let disableAfter = true
      if (this.startDate) {
        // let dateCompBefore = this.dateCompare(this.startDate, fullDate)
        // disableBefore = this.dateCompare(dateCompBefore ? this.startDate : fullDate, nowDate)
        disableBefore = this.dateCompare(this.startDate, nowDate)
      }

      if (this.endDate) {
        // let dateCompAfter = this.dateCompare(fullDate, this.endDate)
        // disableAfter = this.dateCompare(nowDate, dateCompAfter ? this.endDate : fullDate)
        disableAfter = this.dateCompare(nowDate, this.endDate)
      }
      let multiples = this.multipleStatus.data
      let checked = false
      let multiplesStatus = -1
      if (this.range) {
        if (multiples) {
          multiplesStatus = multiples.findIndex((item) => {
            return this.dateEqual(item, nowDate)
          })
        }
        if (multiplesStatus !== -1) {
          checked = true
        }
      }
      let data = {
        fullDate: nowDate,
        year: full.year,
        date: i,
        multiple: this.range ? checked : false,
        beforeMultiple: this.dateEqual(this.multipleStatus.before, nowDate),
        afterMultiple: this.dateEqual(this.multipleStatus.after, nowDate),
        month: full.month,
        lunar: this.getlunar(full.year, full.month, i),
        disable: !(disableBefore && disableAfter),
        isDay
      }
      if (info) {
        data.extraInfo = info
      }

      dateArr.push(data)
    }
    return dateArr
  }
  /**
   * 获取下月天数
   */
  _getNextMonthDays(surplus, full) {
    let dateArr = []
    for (let i = 1; i < surplus + 1; i++) {
      const fullDate = full.year + '-' + (full.month < 10 ?
        (Number(full.month) + 1) : Number(full.month) + 1) + '-' + (i < 10 ?
        '0' + i : i)
      dateArr.push({
        fullDate,
        date: i,
        month: Number(full.month) + 1,
        lunar: this.getlunar(full.year, Number(full.month) + 1, i),
        disable: true
      })
    }
    return dateArr
  }

  /**
   * 获取当前日期详情
   * @param {Object} date
   */
  getInfo(date) {
    if (!date) {
      date = new Date()
    }
    const dateInfo = this.canlender.find(item => item.fullDate === this.getDate(date).fullDate)
    return dateInfo
  }

  /**
   * 比较时间大小
   */
  dateCompare(startDate, endDate) {
    // 计算截止时间
    startDate = new Date(startDate.replace('-', '/').replace('-', '/'))
    // 计算详细项的截止时间
    endDate = new Date(endDate.replace('-', '/').replace('-', '/'))
    if (startDate <= endDate) {
      return true
    } else {
      return false
    }
  }

  /**
   * 比较时间是否相等
   */
  dateEqual(before, after) {
    // 计算截止时间
    before = new Date(before.replace('-', '/').replace('-', '/'))
    // 计算详细项的截止时间
    after = new Date(after.replace('-', '/').replace('-', '/'))
    if (before.getTime() - after.getTime() === 0) {
      return true
    } else {
      return false
    }
  }


  /**
   * 获取日期范围内所有日期
   * @param {Object} begin
   * @param {Object} end
   */
  geDateAll(begin, end) {
    var arr = []
    var ab = begin.split('-')
    var ae = end.split('-')
    var db = new Date()
    db.setFullYear(ab[0], ab[1] - 1, ab[2])
    var de = new Date()
    de.setFullYear(ae[0], ae[1] - 1, ae[2])
    var unixDb = db.getTime() - 24 * 60 * 60 * 1000
    var unixDe = de.getTime() - 24 * 60 * 60 * 1000
    for (var k = unixDb; k <= unixDe;) {
      k = k + 24 * 60 * 60 * 1000
      arr.push(this.getDate(new Date(parseInt(k))).fullDate)
    }
    return arr
  }
  /**
   * 计算阴历日期显示
   */
  getlunar(year, month, date) {
    return CALENDAR.solar2lunar(year, month, date)
  }
  /**
   * 设置打点
   */
  setSelectInfo(data, value) {
    this.selected = value
    this._getWeek(data)
  }
  /**
   * 设置周计划数据
   * @param {Object} data
   * @param {Object} value
   */
  setPlanList(data, value) {
    this.planList = value
    this._getWeek(data)
  }

  /**
   *  获取多选状态
   */
  setMultiple(fullDate) {
    let {
      before,
      after
    } = this.multipleStatus

    if (!this.range) return
    if (before && after) {
      this.multipleStatus.before = ''
      this.multipleStatus.after = ''
      this.multipleStatus.data = []
    } else {
      if (!before) {
        this.multipleStatus.before = fullDate
      } else {
        this.multipleStatus.after = fullDate
        if (this.dateCompare(this.multipleStatus.before, this.multipleStatus.after)) {
          this.multipleStatus.data = this.geDateAll(this.multipleStatus.before, this.multipleStatus.after);
        } else {
          this.multipleStatus.data = this.geDateAll(this.multipleStatus.after, this.multipleStatus.before);
        }
      }
    }
    this._getWeek(fullDate)
  }

  /**
   * 获取每周数据
   * @param {Object} dateData
   */
  _getWeek(dateData) {
    const {
      year,
      month
    } = this.getDate(dateData)

    let firstDay = new Date(year, month - 1, 1).getDay()
    // 调整为周一作为第一天（0=周一，6=周日）
    let adjustedFirstDay = firstDay === 0 ? 6 : firstDay - 1;
    let currentDay = new Date(year, month, 0).getDate()
    let dates = {
      lastMonthDays: this._getLastMonthDays(adjustedFirstDay, this.getDate(dateData)), // 上个月末尾几天
      currentMonthDys: this._currentMonthDys(currentDay, this.getDate(dateData)), // 本月天数
      nextMonthDays: [], // 下个月开始几天
      weeks: []
    }

    let canlender = []
    const surplus = 42 - (dates.lastMonthDays.length + dates.currentMonthDys.length)
    dates.nextMonthDays = this._getNextMonthDays(surplus, this.getDate(dateData))
    canlender = canlender.concat(dates.lastMonthDays, dates.currentMonthDys, dates.nextMonthDays)
    this.canlender = canlender
    let weekTemp = {}
    let weekplan = {} //每周的周计划
    // 拼接数组  上个月开始几天 + 本月天数+ 下个月开始几天
    for (let i = 0; i < canlender.length; i++) {
      //获取本周的周计划数据
      if (i % 7 === 0) {
        //本周开始日期
        const weekBeginDate = canlender[i].fullDate
        // console.log('开始',weekBeginDate)
        //获取计划状态
        weekplan = {}
        for (let i = 0; i < this.planList.length; i++) {
          if (this.planList[i]) {
            const item = this.planList[i]
            // console.log(item.monday)
            if (item.beginDate == weekBeginDate) {
              weekplan = item
              // console.log('weekplan', weekplan)
              break
            }
          }

        }

        weekTemp[parseInt(i / 7)] = {
          weeks: new Array(7),
          planCode: weekplan.planCode,
          planId: weekplan.planId,

          attachIds: weekplan.attachIds,
          // fileUrl: weekplan.fileUrl,
          weekWorkPlan: weekplan.weekWorkPlan,
          workSummary: weekplan.workSummary,
          coordinateHelp: weekplan.coordinateHelp,
          dataState: this.isEmpty(weekplan.dataState) ? weekplan.dataState : 1,
          // dataState: weekplan.dataState //周计划 状态
          subUserNum: weekplan.subUserNum,
          planCommitUserNum: weekplan.planCommitUserNum,
          scanState: weekplan.subUserNum ? weekplan.planCommitUserNum == weekplan.subUserNum ? 2 :
            weekplan.planCommitUserNum == 0 ? 0 : 1 : 'undefined'
        }
      }
      //每天的计划状态
      var dayPlan = {}

      switch (i % 7) {
        case 0: //周一
          // dayPlan.dayStatus = weekplan.mondayStatus
          dayPlan.dayStatus = this.isEmptyWithZero(weekplan.mondayStatus) ? weekplan.mondayStatus : 2
          dayPlan.isHoliday = weekplan.mondayIsHoliday

          dayPlan.subUserNum = weekplan.subUserNum
          dayPlan.dayCommitUserNum = weekplan.mondayCommitUserNum,
            dayPlan.scanState = weekplan.subUserNum ? weekplan.mondayCommitUserNum == weekplan.subUserNum ? 2 : weekplan
            .mondayCommitUserNum == 0 ? 0 : 1: 'undefined'
          break;
        case 1:

          // dayPlan.dayStatus = weekplan.tuesdayStatus
          dayPlan.dayStatus = this.isEmptyWithZero(weekplan.tuesdayStatus) ? weekplan.tuesdayStatus : 2
          dayPlan.isHoliday = weekplan.tuesdayIsHoliday

          dayPlan.subUserNum = weekplan.subUserNum
          dayPlan.dayCommitUserNum = weekplan.tuesdayCommitUserNum,
            dayPlan.scanState = weekplan.subUserNum ? weekplan.tuesdayCommitUserNum == weekplan.subUserNum ? 2 : weekplan
            .tuesdayCommitUserNum == 0 ? 0 : 1: 'undefined'

          break;
        case 2:
          // dayPlan.dayStatus = weekplan.wednesdayStatus
          dayPlan.dayStatus = this.isEmptyWithZero(weekplan.wednesdayStatus) ? weekplan.wednesdayStatus : 2
          dayPlan.isHoliday = weekplan.wednesdayIsHoliday

          dayPlan.subUserNum = weekplan.subUserNum
          dayPlan.dayCommitUserNum = weekplan.wednesdayCommitUserNum,
            dayPlan.scanState = weekplan.subUserNum ? weekplan.wednesdayCommitUserNum == weekplan.subUserNum ? 2 : weekplan
            .wednesdayCommitUserNum == 0 ? 0 : 1: 'undefined'

          break;
        case 3:
          // dayPlan.dayStatus = weekplan.thursdayStatus
          dayPlan.dayStatus = this.isEmptyWithZero(weekplan.thursdayStatus) ? weekplan.thursdayStatus : 2
          dayPlan.isHoliday = weekplan.thursdayIsHoliday

          dayPlan.subUserNum = weekplan.subUserNum
          dayPlan.dayCommitUserNum = weekplan.thursdayCommitUserNum,
            dayPlan.scanState = weekplan.subUserNum ? weekplan.thursdayCommitUserNum == weekplan.subUserNum ? 2 : weekplan
            .thursdayCommitUserNum == 0 ? 0 : 1: 'undefined'

          break;
        case 4:
          // dayPlan.dayStatus = weekplan.fridayStatus
          dayPlan.dayStatus = this.isEmptyWithZero(weekplan.fridayStatus) ? weekplan.fridayStatus : 2
          dayPlan.isHoliday = weekplan.fridayIsHoliday

          dayPlan.subUserNum = weekplan.subUserNum
          dayPlan.dayCommitUserNum = weekplan.fridayCommitUserNum,
            dayPlan.scanState = weekplan.subUserNum ? weekplan.fridayCommitUserNum == weekplan.subUserNum ? 2 : weekplan
            .fridayCommitUserNum == 0 ? 0 : 1: 'undefined'

          break;
        case 5:
          // dayPlan.dayStatus = weekplan.saturdayStatus
          dayPlan.dayStatus = this.isEmptyWithZero(weekplan.saturdayStatus) ? weekplan.saturdayStatus : 2
          dayPlan.isHoliday = weekplan.saturdayIsHoliday

          dayPlan.subUserNum = weekplan.subUserNum
          dayPlan.dayCommitUserNum = weekplan.saturdayCommitUserNum,
            dayPlan.scanState = weekplan.subUserNum ? weekplan.saturdayCommitUserNum == weekplan.subUserNum ? 2 : weekplan
            .saturdayCommitUserNum == 0 ? 0 : 1: 'undefined'

          break;
        case 6:
          // dayPlan.dayStatus = weekplan.sundayStatus
          dayPlan.dayStatus = this.isEmptyWithZero(weekplan.sundayStatus) ? weekplan.sundayStatus : 2
          dayPlan.isHoliday = weekplan.sundayIsHoliday

          dayPlan.subUserNum = weekplan.subUserNum
          dayPlan.dayCommitUserNum = weekplan.sundayCommitUserNum,
            dayPlan.scanState = weekplan.subUserNum ? weekplan.sundayCommitUserNum == weekplan.subUserNum ? 2 : weekplan
            .sundayCommitUserNum == 0 ? 0 : 1: 'undefined'

          break;
      }

      // console.log('swich',dayPlan)
      weekTemp[parseInt(i / 7)].weeks[i % 7] = {
        ...canlender[i],
        ...dayPlan
      }
    }

    this.weeks = weekTemp
    console.log(this.weeks)
  }

  //静态方法
  // static init(date) {
  // 	if (!this.instance) {
  // 		this.instance = new Calendar(date);
  // 	}
  // 	return this.instance;
  // }

  isEmpty(val) {
    if (typeof val === 'number') {
      return false
    }
    if (val === undefined || val === null) {
      return true;
    }
    if (typeof val === 'string' && val.trim() === '') {
      return true;
    }

    return false;
  }

  isEmptyWithZero(val) {
    if (typeof val === 'number' && val === 0) {
      return true
    }
    if (val === undefined || val === null) {
      return true;
    }
    if (typeof val === 'string' && val.trim() === '') {
      return true;
    }

    return false;
  }
}


export default Calendar
